<?php
/**
 * 我的收货地址控制器
 * Created by PhpStorm.
 * User: Jmashimaro
 * Date: 2015/1/21
 * Time: 10:53
 */
class AddressApp extends MallbaseApp {

    /**
     * 收货地址列表
     * @author jiayanxiu
     * @date 2015/02/04
     */
    public function index() {
        $method = strtoupper($_SERVER['REQUEST_METHOD']);
        if ($method != 'GET') {
            $this->ajax_return(FLAG_ERROR, '非法操作！');
        }

        /* 取得列表数据 */
        $user_id = $this->checkLogin();
        if(!$user_id){
            $this->ajax_return(FLAG_ERROR, '用户没有登录！');
        }

        $sql = 'SELECT addr_id,user_id,consignee,region_name,address,phone_mob,`state` FROM '.DB_PREFIX."address WHERE user_id={$user_id}";
        $model = m('address');
        $data = $model->getAll($sql);

        if ($data) {
            $this->ajax_return(FLAG_OK, '', $data);
        } else {
            $this->ajax_return(FLAG_ERROR, '没有收货地址，请您去添加！');
        }
    }

    /**
     * 添加收货地址
     * @author jiayanxiu
     * @date 2015/01/21
     */
    public function add() {

        $method = strtoupper($_SERVER['REQUEST_METHOD']);
        if ($method != 'POST') {
            $this->ajax_return(FLAG_ERROR, '非法操作！');
        }
        $user_id = $this->checkLogin();
        if(!$user_id){
            $this->ajax_return(FLAG_ERROR, '用户没有登录！');
        }
        $data['consignee'] = $this->get_gp('consignee', 'P');
        $data['region_name'] = $this->get_gp('region_name', 'P');
        $data['region_id']   = rand(1,99);
        $data['address'] = $this->get_gp('address', 'P');
        $data['phone_mob'] = $this->get_gp('phone_mob', 'P');
        $data['user_id'] = $user_id;
        if (!$data['consignee']) {
            $this->ajax_return(FLAG_ERROR, '收货人姓名不能为空！');
        }
        if (!$data['region_name']) {
            $this->ajax_return(FLAG_ERROR, '省市地区地址不能为空！');
        }
        if (!$data['address']) {
            $this->ajax_return(FLAG_ERROR, '详细地址不能为空！');
        }
        if (!$data['phone_mob']) {
            $this->ajax_return(FLAG_ERROR, '手机号码不能为空！');
        } elseif(strlen($data['phone_mob']) != 11) {
            $this->ajax_return(FLAG_ERROR, '手机号码长度为11位，请您重新填写！');
        } elseif (!$this->is_number($data['phone_mob'])) {
            $this->ajax_return(FLAG_ERROR, '手机号码格式不正确！');
        }

        $model_address = m('address');

        $address_id = $model_address->add($data);
        if (!$address_id){
            $this->ajax_return(FLAG_ERROR, '添加收货地址失败，请您重试！');
        }

        $this->ajax_return(FLAG_OK, '添加收货地址成功！');
    }

    /**
     * 修改收货地址
     * @author jiayanxiu
     * @date 2015/02/04
     */
    public function edit() {

        $method = strtoupper($_SERVER['REQUEST_METHOD']);
        if ($method != 'POST') {
            $this->ajax_return(FLAG_ERROR, '非法操作！');
        }
        $user_id = $this->checkLogin();
        if(!$user_id){
            $this->ajax_return(FLAG_ERROR, '用户没有登录！');
        }
        $addr_id = $this->get_gp('addr_id', 'P');
        $data['address']     = $this->get_gp('address', 'P');
        $data['phone_mob']   = $this->get_gp('phone_mob', 'P');
        $data['consignee']   = $this->get_gp('consignee', 'P');
        $data['region_name'] = $this->get_gp('region_name', 'P');
        $addr_id = $addr_id ? intval($addr_id) : 0;
        if (!$addr_id) {
            $this->ajax_return(FLAG_ERROR, '不存在的addr_id！');
        }
        if (!$data['consignee']) {
            $this->ajax_return(FLAG_ERROR, '收货人姓名不能为空！');
        }
        if (!$data['region_name']) {
            $this->ajax_return(FLAG_ERROR, '省市地区地址不能为空！');
        }
        if (!$data['address']) {
            $this->ajax_return(FLAG_ERROR, '详细地址不能为空！');
        }
        if (!$data['phone_mob']) {
            $this->ajax_return(FLAG_ERROR, '手机号码不能为空！');
        } elseif(strlen($data['phone_mob']) != 11) {
            $this->ajax_return(FLAG_ERROR, '手机号码长度为11位，请您重新填写！');
        } elseif (!$this->is_number($data['phone_mob'])) {
            $this->ajax_return(FLAG_ERROR, '手机号码格式不正确！');
        }

        $model_address = m('address');

        $flag = $model_address->edit("addr_id = {$addr_id} AND user_id = ".$user_id, $data);
        if ($flag === false) {
            $this->ajax_return(FLAG_ERROR, '操作失败，请您重试！');
        }
        $this->ajax_return(FLAG_OK, '操作成功！');
    }

    /**
     * 删除收货地址
     * @author jiayanxiu
     * @date 2015/02/05
     */
    public function drop() {
        $addr_id = $this->get_gp('addr_id', 'P');
        if (!$addr_id) {
            $this->ajax_return(FLAG_ERROR, '不存在的addr_id');
        }
        $user_id = $this->checkLogin();
        if(!$user_id){
            $this->ajax_return(FLAG_ERROR, '用户没有登录！');
        }
        /* 获取一个类似array(1, 2, 3)的数组 */
        $ids = explode(',', $addr_id);
        $model = m('address');
        $flag = $model->drop("user_id={$user_id} AND addr_id " . db_create_in($ids));
        if ($flag === false) {
            $this->ajax_return(FLAG_ERROR, '操作失败，请您重试！');
        }
        $this->ajax_return(FLAG_ERROR, '操作成功！');
    }

    /**
     * 设置默认收货地址
     * @author jiayanxiu
     * @data 2015/02/06
     */
    public function defaultAddress() {
        $addr_id = $this->get_gp('addr_id', 'P');
        $state     = $this->get_gp('state', 'P');
        $addr_id = $addr_id ? intval($addr_id) : 0;
        $state = $state ? intval($state) : 0;
        if (!$addr_id) {
            $this->ajax_return(FLAG_ERROR, '不存在的addr_id！');
        }
        $user_id = $this->checkLogin();
        if(!$user_id){
            $this->ajax_return(FLAG_ERROR, '用户没有登录！');
        }

        $model = m('address');
        $model->begin();//开启事物
        $flag = $model->edit("user_id={$user_id}", array('state'=>0));
        if ($flag === false) {
            $model->rollback();
            $this->ajax_return(FLAG_ERROR, '操作失败，请您重试！');
        }
        $rs = $model->edit("addr_id={$addr_id} AND user_id={$user_id}", array('state'=>$state));
        if ($rs === false) {
            $model->rollback();
            $this->ajax_return(FLAG_ERROR, '设置默认收货地址失败，请您重试！');
        }
        $model->commit();//提交
        $this->ajax_return(FLAG_OK, '操作成功！');
    }
}